#!/bin/bash

# End-to-End Login Test Runner
# This script runs comprehensive login tests for the RINT Data Manager

set -e

echo "🚀 Starting End-to-End Login Tests"
echo "=================================="

# Check if server is running
echo "📡 Checking server connectivity..."
if ! curl -s http://10.160.43.82:8383/health > /dev/null 2>&1; then
    echo "❌ Server is not running at http://10.160.43.82:8383"
    echo "Please start the server first:"
    echo "  ./target/release/rdm-rust"
    exit 1
fi

echo "✅ Server is running"

# Set test server URL
export TEST_SERVER_URL="http://10.160.43.82:8383"

echo ""
echo "🧪 Running Login Test Suite..."
echo "----------------------------"

# Run the tests (from project root)
cargo test --test login_e2e_test -- --nocapture

echo ""
echo "🎉 All login tests completed successfully!"
echo ""
echo "Test Summary:"
echo "  ✅ End-to-End Idempotent Login Test"
echo "  ✅ Cookie Authentication Test" 
echo "  ✅ Concurrent Login Test"
echo ""
echo "The login system is working correctly with:"
echo "  • JWT token generation and validation"
echo "  • Bearer token authentication"
echo "  • Idempotent login operations"
echo "  • Concurrent request handling"
echo "  • Proper error handling for invalid credentials"